package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.PowerBattery;
import service.PowerBatteryService;

import java.util.List;

@Controller
public class PowerBatteryController {
    @Autowired
    private PowerBatteryService powerBatteryService;


    @GetMapping("/List")
    public String list(Model model){
        List<PowerBattery> powerBatteries = powerBatteryService.PowerList();
        model.addAttribute("list",powerBatteries);
        return "List";
    }

    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        PowerBattery select = powerBatteryService.select(id);
        model.addAttribute("powerBattery",select);
        return "update";
    }
    @PostMapping("/update")
    public String update(Model model, PowerBattery powerBattery){
        int update = powerBatteryService.update(powerBattery);
        if (update>0){
            return "redirect:List";
        }
        return "forward:List";
    }
    @GetMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id){
        int delete = powerBatteryService.delete(id);
        return delete>0?"1":"2";
    }
}
